package com.tourscool.springboot.starter.redisson.health;

import com.tourscool.springboot.starter.redisson.RedissonProperties;
import com.tourscool.springboot.starter.redisson.config.ConfigType;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author Carlton
 * @date: 2018/7/20
 */
@Component
public class RedissonHealthIndicator extends AbstractHealthIndicator {
    @Autowired
    private RedissonClient mRedissonClient;

    @Autowired
    private RedissonProperties mRedissonProperties;

    @Override
    protected void doHealthCheck(Health.Builder builder) throws IOException {
        boolean shutdown = mRedissonClient.isShutdown();
        if (shutdown) {
            builder.down().withDetail("shutdown", true);
        } else {
            final Health.Builder up = builder.up();
            final ConfigType configType = ConfigType.valueOf(mRedissonProperties.getType());
            if (ConfigType.SINGLE.equals(configType)) {
                up.withDetail("clientName", mRedissonClient.getConfig().useSingleServer().getClientName());
            } else if (ConfigType.CLUSTER.equals(configType)) {
                up.withDetail("clientName", mRedissonClient.getConfig().useClusterServers().getClientName());
            } else {
                throw new RuntimeException("Redisson没有找到配置类型:" + mRedissonProperties.getType());
            }
        }
    }
}
